package com.econ.powercloud.ems.controller;

import com.econ.powercloud.common.base.facade.enums.StatusType;
import com.econ.powercloud.common.base.facade.pojo.MsgResult;
import com.econ.powercloud.common.base.facade.pojo.PageResult;
import com.econ.powercloud.ems.aop.UserOperatePower;
import com.econ.powercloud.ems.constant.SysConstant;
import com.econ.powercloud.ems.dal.entity.EmsPeakShift;
import com.econ.powercloud.ems.pojo.EmsEnergyScheduleVO;
import com.econ.powercloud.ems.pojo.EmsPeakShiftDetailVO;
import com.econ.powercloud.ems.pojo.EmsPeakShiftVO;
import com.econ.powercloud.ems.pojo.requestVO.EmsEnergyScheduleRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.EmsPeakShiftRequestVO;
import com.econ.powercloud.ems.pojo.requestVO.EnergyScheduleEnableRequestVO;
import com.econ.powercloud.ems.service.biz.EnergyScheduleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zjr
 * @create 2021/7/21 上午9:52
 * @description
 */
@Api(tags = "能量调度")
@RestController
@RequestMapping(value = "/api/v2/energySchedule")
public class EnergyScheduleController extends ApiBaseController {

    @Autowired
    private EnergyScheduleService energyScheduleService;

    @ApiOperation(value = "获取能量调度信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "energyStationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "energyEquipmentId", value = "储能装置id", dataType = "String")
    })
    @GetMapping(value = "getEnergyScheduleInfo")
    public MsgResult<EmsEnergyScheduleVO> getEnergyScheduleInfo(@RequestParam(value = "energyStationId") String energyStationId, @RequestParam(value = "energyEquipmentId") String energyEquipmentId){
        return MsgResult.<EmsEnergyScheduleVO>builder().data(energyScheduleService.getEnergyScheduleInfo(energyStationId, energyEquipmentId))
                .status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).build();
    }

    @ApiOperation(value = "获取移峰填谷列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "energyStationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "energyEquipmentId", value = "储能装置id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageNo", value = "页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页大小", defaultValue = "20", dataType = "int")
    })
    @GetMapping(value = "getPeakShiftList")
    public MsgResult<PageResult<List<EmsPeakShiftVO>>> getPeakShiftList(@RequestParam(value = "energyStationId") String energyStationId,
                                                                        @RequestParam(value = "energyEquipmentId") String energyEquipmentId,
                                                                        @RequestParam(value = "pageNo", defaultValue = SysConstant.DEFAULT_PAGENO) Integer pageNo,
                                                                        @RequestParam(value = "pageSize", defaultValue = SysConstant.DEFAULT_PAGESIZE) Integer pageSize){
        return MsgResult.<PageResult<List<EmsPeakShiftVO>>>builder().data(energyScheduleService.getPeakShiftList(energyStationId, energyEquipmentId, pageNo, pageSize))
                .status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).build();
    }

    @ApiOperation(value = "更新需量控制信息")
    @PutMapping(value = "modifyDemandControl")
    @UserOperatePower
    public MsgResult modifyDemandControl(@RequestBody EmsEnergyScheduleRequestVO emsEnergyScheduleRequestVO){
        energyScheduleService.modifyDemandControl(emsEnergyScheduleRequestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    /**
     * 这个接口暂时保留,后面的版本这接口被SOC控制替换
     * @param emsEnergyScheduleRequestVO
     * @return
     */
    @ApiOperation(value = "更新后备电源信息")
    @PutMapping(value = "modifyBackupPower")
    @UserOperatePower
    public MsgResult modifyBackupPower(@RequestBody EmsEnergyScheduleRequestVO emsEnergyScheduleRequestVO){
        energyScheduleService.modifyBackupPower(emsEnergyScheduleRequestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation(value = "更新SOC控制")
    @PutMapping(value = "modifySocControl")
    @UserOperatePower
    public MsgResult modifySocControl(@RequestBody EmsEnergyScheduleRequestVO emsEnergyScheduleRequestVO){
        energyScheduleService.modifySocControl(emsEnergyScheduleRequestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation(value = "更新功率反送信息")
    @PutMapping(value = "modifyInversePowerOutput")
    @UserOperatePower
    public MsgResult modifyInversePowerOutput(@RequestBody EmsEnergyScheduleRequestVO emsEnergyScheduleRequestVO){
        energyScheduleService.modifyInversePowerOutput(emsEnergyScheduleRequestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation(value = "增加默认能量调度信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "energyStationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "energyEquipmentId", value = "储能装置id", dataType = "String")
    })
    @PostMapping(value = "addDefaultEnergySchedule")
    public MsgResult addDefaultEnergySchedule(@RequestParam(value = "energyStationId") String energyStationId, @RequestParam(value = "energyEquipmentId") String energyEquipmentId){
        energyScheduleService.addDefaultEnergySchedule(energyStationId, energyEquipmentId);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).build();
    }

    @ApiOperation(value = "增加移峰填谷策略")
    @PostMapping(value = "addPeakShift")
    @UserOperatePower
    public MsgResult addPeakShift(@RequestBody EmsPeakShiftRequestVO emsPeakShiftRequestVO){
        EmsPeakShift emsPeakShift = energyScheduleService.addPeakShift(emsPeakShiftRequestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(emsPeakShift).build();
    }

    @ApiOperation(value = "修改移峰填谷策略")
    @PutMapping(value = "modifyPeakShift")
    @UserOperatePower
    public MsgResult modifyPeakShift(@RequestBody EmsPeakShiftRequestVO emsPeakShiftRequestVO){
        EmsPeakShift emsPeakShift = energyScheduleService.modifyPeakShift(emsPeakShiftRequestVO);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(emsPeakShift).build();
    }

    @ApiOperation(value = "删除移峰填谷策略")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "peakShiftId", value = "移峰填谷策略编号", dataType = "String")
    })
    @DeleteMapping(value = "deletePeakShift")
    @UserOperatePower
    public MsgResult deletePeakShift(@RequestParam(value = "peakShiftId") String peakShiftId){
        energyScheduleService.deletePeakShift(peakShiftId);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation(value = "获取移峰填谷详细策略")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "peakShiftId", value = "移峰填谷策略编号", dataType = "String")
    })
    @GetMapping(value = "getPeakShiftInfo")
    public MsgResult<List<EmsPeakShiftDetailVO>> getPeakShiftInfo(@RequestParam(value = "peakShiftId") String peakShiftId){
        return MsgResult.<List<EmsPeakShiftDetailVO>>builder().data(energyScheduleService.getPeakShiftInfo(peakShiftId))
                .status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).build();
    }

    @ApiOperation(value = "启用/停用需量控制")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "energyStationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "energyEquipmentId", value = "储能装置id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "enabled", value = "启用/停用", dataType = "int")
    })
    @PutMapping(value = "demandControlEnabled")
    @UserOperatePower
    public MsgResult demandControlEnabled(@RequestParam(value = "energyStationId") String energyStationId, @RequestParam(value = "energyEquipmentId") String energyEquipmentId, @RequestParam(value = "enabled") Integer enabled){
        energyScheduleService.demandControlEnabled(energyStationId, energyEquipmentId, enabled);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    /**
     * 这个接口暂时保留,后面的版本这接口被SOC控制替换
     * @return
     */
    @ApiOperation(value = "启用/停用后备电源")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "energyStationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "energyEquipmentId", value = "储能装置id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "enabled", value = "启用/停用", dataType = "int")
    })
    @PutMapping(value = "backupPowerEnabled")
    @UserOperatePower
    public MsgResult backupPowerEnabled(@RequestParam(value = "energyStationId") String energyStationId, @RequestParam(value = "energyEquipmentId") String energyEquipmentId, @RequestParam(value = "enabled") Integer enabled){
        energyScheduleService.backupPowerEnabled(energyStationId, energyEquipmentId, enabled);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    /**
     * 这个是新增的接口，参数采用requestBody方式
     * @return
     */
    @ApiOperation(value = "启用/停用SOC控制")
    @PutMapping(value = "socControlEnabled")
    @UserOperatePower
    public MsgResult socControlEnabled(@RequestBody EnergyScheduleEnableRequestVO requestVO){
        energyScheduleService.socControlEnabled(requestVO.getEnergyStationId(), requestVO.getEnergyEquipmentId(), requestVO.getEnabled());
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation(value = "启用/停用功率反送")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "energyStationId", value = "储能站id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "energyEquipmentId", value = "储能装置id", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "enabled", value = "启用/停用", dataType = "int")
    })
    @PutMapping(value = "inversePowerOutputEnabled")
    @UserOperatePower
    public MsgResult inversePowerOutputEnabled(@RequestParam(value = "energyStationId") String energyStationId, @RequestParam(value = "energyEquipmentId") String energyEquipmentId, @RequestParam(value = "enabled") Integer enabled){
        energyScheduleService.inversePowerOutputEnabled(energyStationId, energyEquipmentId, enabled);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }

    @ApiOperation(value = "启用/停用移峰填谷")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "peakShiftId", value = "移峰填谷ID", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "enabled", value = "启用/停用", dataType = "int")
    })
    @PutMapping(value = "peakShiftEnabled")
    @UserOperatePower
    public MsgResult peakShiftEnabled(@RequestParam(value = "peakShiftId") String peakShiftId, @RequestParam(value = "enabled") Integer enabled){
        energyScheduleService.peakShiftEnabled(peakShiftId, enabled);
        return MsgResult.builder().status(StatusType.OK.getCode()).statusText(StatusType.OK.getStatusText()).data(true).build();
    }
}
